package com.ruoyi.system.kiteUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class FieldChangeDetector {

    /**
     * 检测对象变更并返回详细变更信息(带中文名)
     * @param oldObj 原始对象
     * @param newObj 更新后对象
     * @return 变更详情列表
     */
    public static List<FieldChangeDetail> detectChangesWithChinese(Object oldObj, Object newObj) {
        List<FieldChangeDetail> changes = new ArrayList<>();

        if (oldObj == null || newObj == null || !oldObj.getClass().equals(newObj.getClass())) {
            return changes;
        }

        Field[] fields = oldObj.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                Object oldValue = field.get(oldObj);
                Object newValue = field.get(newObj);

                if (!Objects.equals(oldValue, newValue)) {
                    changes.add(new FieldChangeDetail(field, oldValue, newValue));
                }
            } catch (IllegalAccessException e) {
                System.err.println("无法访问字段: " + field.getName());
            }
        }

        return changes;
    }
}
